#include "importfromcsv.h"
#include "ui_importfromcsv.h"

#include <QMessageBox>
#include <QToolTip>

ImportFromcsv::ImportFromcsv(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ImportFromcsv)
{
    ui->setupUi(this);

    tablewidgetTitle<<"序号"<<"书名"<<"类型"<<"标签类型"<<"全路径"<<"路径"<<"作者"<<"出版社"<<"出版年份"<<"ISBN号"<<"简介"; //表格标题固定
    tablewidgetColumnCount = tablewidgetTitle.size(); //表格列数
    ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows); //只能选中行
    ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);//限制一次只能选中一行，如果没有该限制，按ctrl可以选中多行
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);  //表格数据不能编辑
    ui->tableWidget->setMouseTracking(true);  //鼠标追踪，鼠标悬停到单元格上时显示内容，也可在ui中设置

    //鼠标追踪，鼠标悬停在单元格上时，显示单元格内容
    connect(ui->tableWidget, SIGNAL(entered(QModelIndex)),this,SLOT(tabelTooltip(QModelIndex)));

}

ImportFromcsv::~ImportFromcsv()
{
    delete ui;
}

void ImportFromcsv::tableDataShow(QList<EbookInfo> bookInfoList)
{
    int rowNum = bookInfoList.size();

    ui->tableWidget->setRowCount(0);
    ui->tableWidget->setColumnCount(0);
    ui->tableWidget->clearContents();
    ui->tableWidget->clear();
    ui->tableWidget->setColumnCount(tablewidgetColumnCount);
    ui->tableWidget->setHorizontalHeaderLabels(tablewidgetTitle);
    ui->tableWidget->setColumnWidth(0,38);
    ui->tableWidget->setColumnWidth(1,380);

    //根据数量将数据显示
    ui->tableWidget->setRowCount(rowNum);
    for (int i = 0; i < bookInfoList.size(); ++i)
    {
        ui->tableWidget->setItem(i,0,new QTableWidgetItem(QString::number(i+1)));
        ui->tableWidget->setItem(i,1,new QTableWidgetItem(bookInfoList[i].bookname));
        ui->tableWidget->setItem(i,2,new QTableWidgetItem(bookInfoList[i].booktype));
        ui->tableWidget->setItem(i,3,new QTableWidgetItem(bookInfoList[i].booklabel));
        ui->tableWidget->setItem(i,4,new QTableWidgetItem(bookInfoList[i].bookpath));
        ui->tableWidget->setItem(i,5,new QTableWidgetItem(bookInfoList[i].bookdir));
        ui->tableWidget->setItem(i,6,new QTableWidgetItem(bookInfoList[i].author));
        ui->tableWidget->setItem(i,7,new QTableWidgetItem(bookInfoList[i].publisher));
        ui->tableWidget->setItem(i,8,new QTableWidgetItem(bookInfoList[i].publishyear));
        ui->tableWidget->setItem(i,9,new QTableWidgetItem(bookInfoList[i].bookisbn));
        ui->tableWidget->setItem(i,10,new QTableWidgetItem(bookInfoList[i].bookintroduction));
    }

    m_importBookinfo = bookInfoList;
    ui->labelImportNum->setText(QString("数目：%1").arg(rowNum));
}

//鼠标悬停在单元格中时显示单元格内容
void ImportFromcsv::tabelTooltip(QModelIndex index)
{
    QToolTip::showText(QCursor::pos(),index.data().toString());
}

void ImportFromcsv::on_ptnConfirmImport_clicked()
{
    QMessageBox msgBox(QMessageBox::Information, tr("信息"),tr("是否已确认无误？确认将数据导入吗？")
                       ,QMessageBox::Yes|QMessageBox::No,this);
    if (msgBox.exec() == QMessageBox::Yes)
    {
        if (m_importBookinfo.isEmpty())
        {
            QMessageBox msgBox(QMessageBox::Warning, tr("警告"),tr("导入的数据为空，确认导入将清空所有数据，确定要执行此操作吗？")
                               ,QMessageBox::Yes|QMessageBox::No,this);
            if (msgBox.exec() == QMessageBox::No)
            {
                return;
            }
        }
        emit sendImportData(m_importBookinfo);
        this->hide();
    }
}

void ImportFromcsv::on_ptnCancelImport_clicked()
{
    this->hide();
}
